1 //------------------------------------------------------------------------------
2 // <copyright file="Tk.cs" company="Microsoft">
4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
11 // You must not remove this notice, or any other, from this software.
14 //------------------------------------------------------------------------------
17 using System
.Collections
;
18 using System
.Runtime
.InteropServices
;
20 namespace SharedSourceCLI
.TK
22 public class TclException
: Exception
{
24 public TclException() {
28 public struct TclCmdToken
{
29 internal IntPtr _token
;
32 public delegate void TclAppInitProc(TclInterp interp
);
33 public delegate void TclCmdProc(TclInterp interp
, string[] argv
);
34 public delegate void TclCmdDeleteProc();
36 public class TclInterp
{
38 // innner unmanaged object
39 unsafe Tcl_Interp
* _interp
;
41 // hashtable to keep marshalled delegates alive
42 private Hashtable _keepAlive
;
46 _interp
= TclNative
.Tcl_CreateInterp();
48 throw new OutOfMemoryException();
50 _keepAlive
= new Hashtable();
53 internal void AddKeepAlive(Object o
) {
54 _keepAlive
.Add(o
, null);
57 internal void RemoveKeepAlive(Object o
) {
61 internal void HandleError(int res
) {
62 if (res
!= TclNative
.TCL_OK
)
63 throw new TclException();
66 public void TclInit() {
68 HandleError(TclNative
.Tcl_Init(_interp
));
72 public void TkInit() {
74 HandleError(TkNative
.Tk_Init(_interp
));
78 public void TkMain(TclAppInitProc initProc
, string[] argv
) {
79 TclAppInitProcWrapper initWrapper
= new TclAppInitProcWrapper(this, initProc
);
82 TkNative
.Tk_MainEx(argv
.Length
, argv
, initWrapper
.Callback
, _interp
);
85 GC
.KeepAlive(initWrapper
);
89 public void TkMain(TclAppInitProc initProc
) {
90 TkMain(initProc
, Environment
.GetCommandLineArgs());
93 public void Eval(string str
) {
95 HandleError(TclNative
.Tcl_Eval(_interp
, str
));
99 public string GetVar(string varName
, int flags
) {
101 return Marshal
.PtrToStringAnsi(TclNative
.Tcl_GetVar(_interp
, varName
, flags
));
105 public string GetVar(string varName
) {
106 return GetVar(varName
, 0);
109 public TclCmdToken
CreateCommand(string cmdName
, TclCmdProc proc
, TclCmdDeleteProc deleteProc
) {
112 TclCmdProcWrapper cmdWrapper
= new TclCmdProcWrapper(this, proc
);
113 TclCmdDeleteProcWrapper deleteWrapper
= new TclCmdDeleteProcWrapper(this, deleteProc
, cmdWrapper
);
116 token
._token
= TclNative
.Tcl_CreateCommand(_interp
, cmdName
, cmdWrapper
.Callback
,
117 IntPtr
.Zero
, deleteWrapper
.Callback
);
123 public TclCmdToken
CreateCommand(string cmdName
, TclCmdProc proc
) {
124 return CreateCommand(cmdName
, proc
, null);